package day14;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author joker
 * @date 2022/4/24 0024 - 11:45
 */
public class ProxyFactory {
    public static Object getProxyInstance(Object obj){
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("代理类前");
                        Object invoke = method.invoke(obj, args);
                        System.out.println("代理类后");
                        return invoke;
                    }
                });
    }

    public static void main(String[] args) {
        Person man = new Man();
        Person proxyInstance = (Person) ProxyFactory.getProxyInstance(man);
        proxyInstance.m1();
        String hello = proxyInstance.m2("hello");
        System.out.println(hello);
    }

}
